package com.waves.spring.boot.constructor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;

import java.util.Arrays;

/**
 * 添加 ApplicationContext 初始化器
 */
//@Configuration
public class Config3 {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Config3.class);
        application.addInitializers(applicationContext -> {
            if (applicationContext instanceof GenericApplicationContext) {
                GenericApplicationContext context = (GenericApplicationContext) applicationContext;
                context.registerBean("bean3", Bean3.class);
            }
        });
        ConfigurableApplicationContext context = application.run(args);
        Arrays.stream(context.getBeanDefinitionNames()).forEach(i -> {
            System.out.println("name: " + i +
                    " 来源: " + context.getBeanFactory().getBeanDefinition(i).getResourceDescription());
        });
        context.close();
    }

//    @Bean
    public Bean1 getBean1() {
        return new Bean1();
    }

//    @Bean
    public TomcatServletWebServerFactory servletWebServerFactory() {
        return new TomcatServletWebServerFactory();
    }


}

